package be.darling.scrt.statistics;

import be.darling.scrt.data.Condition;
import be.darling.scrt.data.Randomization;
import be.darling.scrt.data.Value;
import be.darling.scrt.data.ValueComparator;

import java.util.Collections;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: geert
 * Date: Nov 22, 2007
 * Time: 1:48:25 PM
 * To change this template use File | Settings | File Templates.
 */
public class MedianDifference extends SimpleStatistic{
    public MedianDifference()
    {}

    public double calc(Randomization randomization){ // return Comparable object? {
        //return Math.abs(randomization.getMeans(label1).getMean() - randomization.getMeans(label2).getMean());
        int index = 0;

        List<Value> l1 = randomization.getValues(getLabel1());
        List<Value> l2 = randomization.getValues(getLabel2());
        Collections.sort(l1, new ValueComparator(index));
        Collections.sort(l2, new ValueComparator(index));
        // todo algemener, ook percentiel x
        double m1 =0;
        if (l1.size() %2 == 1 ){
            m1 = l1.get(((l1.size()-1) / 2)).getValue(index);
            //System.out.println("Median 1 (simple)" +m1);
        } else {
            int first =l1.size()/2 -1;
            m1 = (l1.get(first).getValue(index) + l1.get(first+1).getValue(index)) / 2;
            //System.out.println("Median 1 " +l1.get(first).getValue(index) +"\t"+ l1.get(first+1).getValue(index)+"\t"+m1);
        }

        double m2 =0;
        if (l2.size() %2 == 1 ){
            m2 = l2.get(((l2.size()-1) / 2)).getValue(index);
            //System.out.println("Median 2 (simple)" +m2);
        } else {
            int first =l2.size()/2 -1;
            m2 = (l2.get(first).getValue(index) + l2.get(first+1).getValue(index)) / 2;
            //System.out.println("Median 2 " +l2.get(first).getValue(index) +"\t"+ l2.get(first+1).getValue(index)+"\t"+m2);
        }

        double diff = m1 -m2;
        if (isAbsolute()) {
            diff = Math.abs(diff);
        }
        return diff;


    }


}